package utils.nfc
{

	
	import components.base.NfcBorder;
	
	import flash.net.getClassByAlias;
	
	import mx.collections.XMLListCollection;
	
	import utils.config.Config;
	import utils.filemanager.*;
	import utils.log.LOGGER;
	import utils.nfc.*;
	import utils.nfc.xml.*;

	/*
	* 	Diese Klasse stellt die Schnittstelle zur NFC Hardware dar
	*	Diese wird mittels XML dateien realisiert, so dass es zu einer asynchronen
	* 	Kommunikation zwischen Hardware und Software mittels Dateien kommt.
	*	NfcInterface bietet unterschiedliche statische Methoden, die es erlauben
	*	direkt den Inhalt abstrahiert in eine vorhandene XML Struktur zu schreiben
	*/
	
	
	public class NfcInterface
	{
	// tagID = position
		private static var POSITIONS_ARRAY:Array 	= [0,1,2,3];
		public static var POSITION_TOP:int 			= 0;
		public static var POSITION_RIGHT:int 		= 1;
		public static var POSITION_BOTTOM:int 		= 2;
		public static var POSITION_LEFT:int 		= 3;
		
		public static const TECHNOLOGY_ISO:String		= "ISO-14443";
		
		public function NfcInterface()
		{
		}
		
		// leert die XML und fügt nur die in maximale Anzahl von NfcTags hinzu mit leerem record und leutenden LEDs
		public static function clearNfcXml():void{
			// NFC Rand leeren
			NfcBorder.getInstance().deactivateAll();
			
			var leds:Array;
			// LEDs aus wenn NFC nicht erlaubt ist			
			if(Config.isNfcEnabled()){
			 	leds=[true,true,true,true];
			}else{
				leds=[false,false,false,false];
			}
			
			var nfcTagsArray:Array = new Array();
			for (var pos:int=0; pos<POSITIONS_ARRAY.length; pos++)
			{
				if(leds[pos])
					nfcTagsArray.push(new NfcTag(pos, Led.STATUS_PERMANENT_RED,"",null,NfcTag.MODE_READER));
				else
					nfcTagsArray.push(new NfcTag(pos, Led.STATUS_OFF,"",null,NfcTag.MODE_READER));
			}
			LOGGER.log("clearNfcXML() called");
			var blankNfcContent:NfcTags = new NfcTags(nfcTagsArray,"","",false);
			if (!equals(blankNfcContent.toXML(),getNfcContentAsXml())){
				MyFileWriter.writeNfcContent(blankNfcContent.toXML());
			}
			
		}

		public static function getNfcContentAsXml():XML{
			var currentXML:XML = new XML(MyFileWriter.read(MyFileWriter.XML_FILEPATH));
			return currentXML;
		}
		
		
		public static function createNfcContent(objArr:Array,pageID:String = "0", technology:String = "ISO-14443"):void{ 
			// wenn NFC nicht enabled ist, keinen Inhalt schreiben
			if(!Config.isNfcEnabled()){
				return;
			}
			// den NFC Rand aktualisieren
			NfcBorder.getInstance().updateNFCBorder(objArr);
			
			
			if (objArr.length < 1){
				LOGGER.log("Error: No objects in given Array");
				clearNfcXml();
				return;
			}
			if(objArr[0] == null){
				clearNfcXml();
				return;
			}
			
			var nfcTagsArray:Array = new Array();
			
			for (var j:int = 0; j < 4; j++) 
			{
				for (var i:int = 0; i < objArr.length; i++) 
				{
					if (objArr[i] == null)
					{
						// blank NFC Tag erzeugen
						nfcTagsArray[j] = new NfcTag(j, Led.STATUS_PERMANENT_RED ,"",null,NfcTag.MODE_READER);
						break;
					}
					if (j == objArr[i].tagID){
						switch(objArr[i].recordType)
						{
							case RecordType.RECORD_TYPE_URL:
							{
								nfcTagsArray[j] = new NfcTag(objArr[i].tagID,Led.STATUS_PERMANENT_GREEN,objArr[i].contentRefID,RecordType.getRecord(objArr[i],objArr[i].recordType),objArr[i].mode);
								break;
							}
							case RecordType.RECORD_TYPE_MAPURL:
							{
								nfcTagsArray[j] = new NfcTag(objArr[i].tagID,Led.STATUS_PERMANENT_GREEN,objArr[i].contentRefID,RecordType.getRecord(objArr[i],RecordType.RECORD_TYPE_URL),objArr[i].mode);
								break;
							}
							case RecordType.RECORD_TYPE_EVENT:
							{
								nfcTagsArray[j] = new NfcTag(objArr[i].tagID,Led.STATUS_PERMANENT_GREEN,objArr[i].contentRefID,RecordType.getRecord(objArr[i],RecordType.RECORD_TYPE_URL),objArr[i].mode);
								break;
							}
							case RecordType.RECORD_TYPE_CALL:
							{
								nfcTagsArray[j] = new NfcTag(objArr[i].tagID,Led.STATUS_PERMANENT_GREEN,objArr[i].contentRefID,RecordType.getRecord(objArr[i],objArr[i].recordType),objArr[i].mode);
								break;
							}
							case RecordType.RECORD_TYPE_EMAIL:
							{
								nfcTagsArray[j] = new NfcTag(objArr[i].tagID,Led.STATUS_PERMANENT_GREEN,objArr[i].contentRefID,RecordType.getRecord(objArr[i],objArr[i].recordType),objArr[i].mode);
								break;
							}
							case RecordType.RECORD_TYPE_VCARD:
							{
								nfcTagsArray[j] = new NfcTag(objArr[i].tagID,Led.STATUS_PERMANENT_GREEN,objArr[i].contentRefID,RecordType.getRecord(objArr[i],objArr[i].recordType),objArr[i].mode);
								break;
							}
						}
						break;
					} else{
						// blank NFC Tag erzeugen
						nfcTagsArray[j] = new NfcTag(j, Led.STATUS_PERMANENT_RED,"",null,NfcTag.MODE_READER);						
					}
				}
			}
			
			
			
			var rootXml:XML = new NfcTags(nfcTagsArray,pageID,technology).toXML();

			if (!equals(rootXml,NfcInterface.getNfcContentAsXml())){
				MyFileWriter.writeNfcContent(rootXml);
			}
				
		}
		
		private static function equals(xml1:XML,xml2:XML):Boolean{
			var xml1c:XML = new XML(xml1);
			var xml2c:XML = new XML(xml2);
			xml1c.@lastChange ="";
			xml2c.@lastChange ="";
			
			return (xml1c.toString() == xml2c.toString());
		}
		
		
		public static function switchNfcTagToMode(newMode:String,tagID:Number):void{
			var contentXML:XML = getNfcContentAsXml();
			trace(contentXML.child("nfcTag"));
			var xmlList:XMLList;
			
			for (var i:int = 0; i < contentXML.child("nfcTag").length; i++) 
			{
				if(contentXML.child("nfcTag")[i].attribute("tagID") == tagID){
					if(contentXML.child("nfcTag")[i].attribute("mode") != NfcTag.MODE_READER){
						contentXML.child("nfcTag")[i].@mode = newMode;
					}
				}
			}
			MyFileWriter.writeNfcContent(contentXML);
		}
		
		public static function generateAllURLContent(url:String,contentID:String=""):Array{
			var urls:Array = new Array();
			for (var i:int = 0; i < POSITIONS_ARRAY.length; i++) 
			{
				urls.push(new NfcTagUrl(i,url,contentID+":"+i));
			}
			return urls;
		}
/*		public static function generateAllVCARDContent(emp:EmployeeVO,contentID:String=""):Array{
			var urls:Array = new Array();
			for (var i:int = 0; i < POSITIONS_ARRAY.length; i++) 
			{
				urls.push(new NfcTagVcard(i,emp,contentID+":"+i));
			}
			return urls;
		}*/
		
		
		
		private static function addNfcContent(type:String, contentObject:Object, clearBefore:Boolean=false):void{
			if (clearBefore)
				clearNfcXml();
			var currentXML:XML = getNfcContentAsXml();
			// to continue
			
		}
		
		
	}
}